package com.freshcity.page.welcome
{
	import com.freshcity.core.BasePage;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.welcome.FirstTestWin;
	import com.freshcity.welcome.WelcomeWin;
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	import com.greensock.easing.Linear;
	
	import fl.controls.ComboBox;
	import fl.controls.RadioButtonGroup;
	
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class DataInit extends BasePage
	{
		public var loginUser:Object;
		
		private var sexGroup:RadioButtonGroup;
		private var myCity:ComboBox;

		private var step1:FirstTestWin;
		private var step2:WelcomeWin;
		private var rbg:RadioButtonGroup;
		

		override public function initialize():void
		{
			super.initialize();
			showWelcomePage();
		}
		
		private function showFirstPage():void
		{
			if(step2.parent)step2.parent.removeChild(step2);
			
			step1 = new FirstTestWin();
			LayerManager.popupLayer.addChild(step1);
			rbg=new RadioButtonGroup("sex");
			step1.femaleRadio.group=this.rbg;
			step1.maleRadio.group=this.rbg;
			
			step1.x = (stage.stageWidth-step1.width)/2;
			step1.y = (stage.stageHeight-step1.height)/2;
			
			step1.submitBtn.addEventListener(MouseEvent.CLICK, onStep2SubmitBtnHandler);
		}
		
		private function showWelcomePage():void
		{
			step2=new WelcomeWin();
			LayerManager.popupLayer.addChild(step2);
			step2.x = (stage.stageWidth-step2.width)/2;
			step2.y = (stage.stageHeight-step2.height)/2;
			
			for (var i:int = 1; i < 13; i++) 
			{
				step2["tf"+i].alpha=0;
			}
			
			var timeLine:TimelineLite = new TimelineLite({onComplete:showFirstPage});
			timeLine.append(new TweenLite(step2["tf1"], 0.8, {alpha: 1,delay:0.3}));
			for (var j:int = 2; j < 13; j++) 
			{
				timeLine.append(new TweenLite(step2["tf"+j], 0.8, {alpha: 1,delay:1.6}));
			}
			timeLine.append(new TweenLite(step2,2.8,{alpha:0,delay:5}));
			//timeLine.play();
			
		}
		
		private function sendServer():void
		{
			loginUser.sex=this.rbg.selection.value;
			loginUser.selfDesc="";
			loginUser.cond={};
			EffectManager.addLoadMask();
			commu.send(OpCodes.INIT_DATA, loginUser, onDataInitHandler);
		}
		
		private function onDataInitHandler(data:Object):void
		{
			commu.unRegister(OpCodes.INIT_DATA);
			//welcome.show(data);
			commu.send(OpCodes.ENTRY, data, onLoad);
			//TweenLite.to(welcome.win, 0.3, {onComplete: hide,x: "-800", delay: 0.3});			
		}
		
		private function onStep2SubmitBtnHandler(event:Event):void
		{
			this.destroy();
			sendServer();
		}
		
		override public function destroy():void
		{
			if(step1.parent)step1.parent.removeChild(step1);
			if(step2.parent)step2.parent.removeChild(step2);
		}

		private function onLoad(data:*):void
		{
			EffectManager.hideLoadMask();
			TweenLite.to(this.step2, 0.3, {onComplete: destroy, x: "-800"});
			var main:MainScene=new MainScene();
			main.owningGroup=this.owningGroup;
			main.user=  data.user;
			main.cond = data.cond;
			GlobalVar.LOGIN_USER=main.user;
			main.initialize();
			
			//finishGuide();
		}
		
		private function finishGuide():void
		{
			EffectManager.hideUIMask();
			var s:String = "非常棒！你已经完成了基础的工作。如何找到属于自己的有缘人，下来就交给你了。";
			guideTip.getGuideTips(s,finishGuide2);
		}		

		private function finishGuide2():void
		{
			var s:String = "点击“纸飞机”或“寻觅”栏目看看吧。说不定好运正等着你呢，谁知道呢...";
			guideTip.getGuideTips(s,guideTip.hideGuideTips);
		}		
	}
}
